﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using PureMVC.Patterns;
using PureMVC.Interfaces;

using GEF2.Model;
//using GEF2.View.Components;
using GEF2.Model.Storage;
using GEF2;

namespace GEF2.View
{
    public class EntityListMediator : Mediator, IMediator
    {
		private EntityProxy entityProxy;

		public new const string NAME = "EntityListMediator";

        public EntityListMediator(System.Windows.Forms.ListBox viewComponent)
			: base(NAME, viewComponent)
		{
        //    MainWindow.AddEntity += new EventHandler(MainWindow_AddEntity);
		}

		public override void OnRegister()
		{
			base.OnRegister();
            entityProxy = (EntityProxy)Facade.RetrieveProxy(EntityProxy.NAME);
		}

        private System.Windows.Forms.ListBox ListBox
        {
            get { return (System.Windows.Forms.ListBox)ViewComponent; }
        }

		public override IList<string> ListNotificationInterests()
		{
			IList<string> list = new List<string>();
            list.Add(ApplicationFacade.ENTITY_ADDED);
            list.Add(ApplicationFacade.ENTITY_DELETED);
            return list;
		}

        private void UpdateEntitys()
        {
            if (ListBox.InvokeRequired)
            {
                ListBox.Invoke(new MethodInvoker(() => { UpdateEntitys(); }));
            }
            else
            {
                ListBox.Items.Clear();
                foreach (Entity entity in entityProxy.Entitys)
                {
                    ListBox.Items.Add(entity.Name);
                }
            }
        }

		public override void HandleNotification(INotification note)
		{
			switch (note.Name)
			{
                case ApplicationFacade.ENTITY_ADDED:
                case ApplicationFacade.ENTITY_DELETED:
                    {
                        UpdateEntitys();
                    }
                    break;
			}
		}

    }
}
